#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    //完成初始化设置
    //设置这些按钮的图标
    ui->pushButton_target->setIcon(QIcon(":/image/target.png"));
    ui->pushButton_target->setIconSize(QSize(300,300));

    ui->pushButton_up->setIcon(QIcon(":/image/up.png"));
    ui->pushButton_up->setIconSize(QSize(30,30));

    ui->pushButton_down->setIcon(QIcon(":/image/down.png"));
    ui->pushButton_down->setIconSize(QSize(30,30));

    ui->pushButton_left->setIcon(QIcon(":/image/left.png"));
    ui->pushButton_left->setIconSize(QSize(30,30));

    ui->pushButton_right->setIcon(QIcon(":/image/right.png"));
    ui->pushButton_right->setIconSize(QSize(30,30));

    //设置快捷键
    //在widget的构造函数中进行设置,程序一启动，快捷键就是有效的
    //方式一:通过按键的名字来设置,虽然简单，单容易出错
//    ui->pushButton_up->setShortcut(QKeySequence("w"));
//    ui->pushButton_down->setShortcut(QKeySequence("s"));
//    ui->pushButton_left->setShortcut(QKeySequence("a"));
//    ui->pushButton_right->setShortcut(QKeySequence("d"));
    //还可以通过按键名字来设置组合键来作为按键的快捷键(大小写字母要拼写正确且不能有空格隔开),eg:ctrl+W
    //ui->pushButton_up->setShortcut(QKeySequence("Ctrl+w"));

    //方式二:通过按键的枚举来设置快捷键
    ui->pushButton_up->setShortcut(QKeySequence(Qt::Key_W));
    ui->pushButton_down->setShortcut(QKeySequence(Qt::Key_S));
    ui->pushButton_left->setShortcut(QKeySequence(Qt::Key_A));
    ui->pushButton_right->setShortcut(QKeySequence(Qt::Key_D));
    //还可以通过按键枚举的方式设置组合键来作为按键的快捷键,eg:ctrl+W
    //ui->pushButton_up->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_W));

    //开启鼠标点击的连发功能(键盘的连发默认是支持的)
    ui->pushButton_up->setAutoRepeat(true);
    ui->pushButton_down->setAutoRepeat(true);
    ui->pushButton_left->setAutoRepeat(true);
    ui->pushButton_right->setAutoRepeat(true);
}

Widget::~Widget()
{
    delete ui;
}


void Widget::on_pushButton_up_clicked()
{
    //获取到target原本的位置
    QRect rect = ui->pushButton_target->geometry();
    //基于上次的位置,再设置新的位置
    ui->pushButton_target->setGeometry(rect.x() , rect.y() - 5 , rect.width() , rect.height());
}

void Widget::on_pushButton_down_clicked()
{
    //获取到target原本的位置
    QRect rect = ui->pushButton_target->geometry();
    //基于上次的位置,再设置新的位置
    ui->pushButton_target->setGeometry(rect.x() , rect.y() + 5 , rect.width() , rect.height());
}

void Widget::on_pushButton_left_clicked()
{
    //获取到target原本的位置
    QRect rect = ui->pushButton_target->geometry();
    //基于上次的位置,再设置新的位置
    ui->pushButton_target->setGeometry(rect.x() - 5 , rect.y() , rect.width() , rect.height());
}

void Widget::on_pushButton_right_clicked()
{
    //获取到target原本的位置
    QRect rect = ui->pushButton_target->geometry();
    //基于上次的位置,再设置新的位置
    ui->pushButton_target->setGeometry(rect.x() + 5 , rect.y() , rect.width() , rect.height());
}
